// /////////////////////////////////////////////////////////////////////////////
//
// Name:            SerializedAttribute.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "SerializedAttribute.h"


// SerializedAttribute class
// SerializedAttribute constructor.
SerializedAttribute::SerializedAttribute(const std::string &name, const std::string &type)
: mName(name), mType(type)
{
}

// SerializedAttribute deconstructor.
SerializedAttribute::~SerializedAttribute()
{
}

// Gets the name of this attribute.
const std::string& SerializedAttribute::getName() const
{
    return mName;
}

// Gets the type of this attribute as a string.
const std::string& SerializedAttribute::getType() const
{
    return mType;
}

// Removes all formatting from the given string in order to extract the base values of the
// attribute.
std::string SerializedAttribute::removeStringFormatting(const std::string &value, const std::string &type)
{
    // Determine the location of the base elements of an attribute string (type name, '(' and ')').
    unsigned int typeLocation = value.find(type);
    unsigned int leftBracketLocation = value.find("(");
    unsigned int rightBracketLocation = value.rfind(")");

    // Determine the type of the attribute this string describes.
    if(typeLocation == std::string::npos || leftBracketLocation == std::string::npos ||
       rightBracketLocation == std::string::npos)
      return std::string("");

    // Make sure that the base elements are in the right order.
    if(typeLocation > leftBracketLocation || leftBracketLocation > rightBracketLocation)
      return std::string("");

    // Create a new string without formatting.
    stringstream ss1(value.substr(leftBracketLocation+1, rightBracketLocation-leftBracketLocation-1));
    stringstream ss2;

    while(!ss1.eof())
    {
        std::string tempString;
        ss1 >> tempString;

        while(tempString.find(",") != std::string::npos)
          tempString.replace(tempString.find(","), 1, " ");

        ss2 << tempString << " ";
    }

    // Return the new stringstream.
    return ss2.str();
}

// End of File
